package top.wardyan.wdev.dbdevassist.action.proc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.LinkedList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Record;

import top.wardyan.base.util.DateUtil;
import top.wardyan.util.Msg;
import top.wardyan.wdev.dbdevassist.action.base.BaseMasterDetailsEditShell;
import top.wardyan.wdev.dbdevassist.model.YTObjectCol;
import top.wardyan.wdev.dbdevassist.model.YTObjectPara;

@SuppressWarnings("unused")
public class ProcEditShell extends BaseMasterDetailsEditShell<ProcEditShell>{

	
	
//	public ProcEditShell() {
//		this(1, "gp_qryreceive_item");
//	}
	
	public ProcEditShell(int sID, String xName) {
		super(sID, xName, 3);
		this.createContent();
	}
	
	private void createContent() {
		
		
	}

	
	
	

	



	

	


	
	@Override
	protected void getParas() {
		List<Record> rs = Db.use().find("select p.name, p.parameter_id, p.system_type_id from sys.parameters AS p INNER JOIN sys.objects as b on b.object_id = p.object_id where b.name = ?",  xName);
		if(rs != null) {
			for(Record r : rs) {
				String name = r.getStr("name");
				YTObjectPara para = paraService.find(self.sID, name);
				if(para == null) {
					para = new YTObjectPara().setMID(self.sID).setRAuthorID(dda.user.getSID())
							.setRDicID(0).setSCreateTime(DateUtil.getCurrentTimestamp()).setSLastUpdate(DateUtil.getCurrentTimestamp())
							.setSStatus(2).setSVersion(1).setXDesc("").setXField(name).setXHint("").setXMainName(xName).setXName("")
							.setXNote("");
							//.setXTableName("").setXType(0);
					paraService.save(para);
				}
			}
		}
		this.refreshParas();
	}

	@Override
	protected void getCols() {
		List<Record> rs = Db.use().find("select p.name, p.parameter_id, p.system_type_id from sys.parameters AS p INNER JOIN sys.objects as b on b.object_id = p.object_id where b.name = ?",  xName);
		List<String> paralist = new LinkedList<>();
		StringBuilder sb = new StringBuilder();
		if(rs != null) {
			for(Record r : rs) {
				sb.append("?");
				if(!r.equals(rs.get(rs.size() - 1))) {
					sb.append(",");
				}
			}
		}
		String name = "{call " + xName + "(" + sb.toString() + ")}";
		//System.out.println(name);
		Db.use().execute(new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				java.sql.CallableStatement proc = conn.prepareCall(name);
				for(int i = 1; i <= rs.size(); i++) {
					proc.setObject(i, Types.NULL);
				}
				//proc.execute();
				ResultSet rs = null;
				try {
					rs = proc.executeQuery();
				}catch(Exception e) {
					Msg.show("错误", "该过程没有输出数据集");
				}
				
				if(rs != null) {
					java.sql.ResultSetMetaData metadata = rs.getMetaData();
					for(int i = 1; i <= metadata.getColumnCount(); i++) {
						String name = metadata.getColumnName(i);
						//metadata.getColumnTypeName(column)
						// 没有考虑到数据类型!
						//SQLite.getInstance().tablecolService
						YTObjectCol c = dda.objectColService.find(self.sID, name);
						if(c == null) {
							c = new YTObjectCol().setMID(self.sID)
									.setRAuthorID(dda.user.getSID())
									.setRDicID(0).setSStatus(2)
									.setSVersion(1).setXDesc("").setXField(name).setXHint("")
									.setXMainName(xName).setXName("").setXNote("")
									.setXType(0).setXTableName("")
									;
							dda.objectColService.save(c);
						}
					}
				}
				return null;
			}
		});
		this.refreshCols();
	}

	@Override
	protected int getTypeID() {
		return 10;
	}
	
}
